﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace Rafy.Book.Converters
{
    public class EqualsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.ToString() == parameter?.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? parameter?.ToString() : Binding.DoNothing;
        }
    }
    /// <summary>
    /// 学科选择转换器
    /// </summary>
    public class SelectTagConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Console.WriteLine($"Convert={value?.ToString()}== {parameter?.ToString()}");
            return value?.ToString() == parameter?.ToString() ? "Selected" : null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

    public class ShowToVisibleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(value is int val && int.TryParse(parameter.ToString(),out int id))
            {
                return val == id ? Visibility.Visible : Visibility.Collapsed;
            }
            return Visibility.Collapsed;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
    public class ChkBorderBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Brushes.Red : Brushes.Transparent;  // 选中时红色，未选中时透明
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class CharWidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if(!string.IsNullOrEmpty(value.ToString()))
            {
                int count = value.ToString().Length;
                //Console.WriteLine($"{value.ToString()}={count}");

                if (count > 7)
                {
                    return count * 14;
                }
                else if (count > 4)
                {
                    return count * 15;
                }
                else
                    return 70;
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }

    public class CharSpacingConverter : IValueConverter
    {
        //还是要使用封装控件
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is BookWordModel word)
            {
                string character = word.Character;
                string pinyin = word.Pinyin;
                if (pinyin.Length > 6)
                {
                    //Console.WriteLine($"{character}={character.Length},拼音长={pinyin.Length}");
                    return string.Join(new string(' ', 2), character.ToCharArray());
                }
                else
                    return string.Join(new string(' ',2), character.ToCharArray());

            }

            return "";
        }

        // 反向转换（可选，这里保留原值）
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }


}
